package com.example.jobback.config;


import com.example.jobback.intercept.ManagerIntercept;
import com.example.jobback.intercept.RecruiterIntercept;
import com.example.jobback.intercept.SeekerIntercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
 * @ClassName: WebConfig
 * @Description: web配置类
 * @Author: lhb
 * @Date: 2025/5/12
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    private final SeekerIntercept seekerIntercept;

    private final RecruiterIntercept recruiterIntercept;

    private final ManagerIntercept managerIntercept;


    @Autowired
    WebConfig(SeekerIntercept seekerIntercept,
              RecruiterIntercept recruiterIntercept,
              ManagerIntercept managerIntercept){
        this.seekerIntercept = seekerIntercept;
        this.recruiterIntercept = recruiterIntercept;
        this.managerIntercept = managerIntercept;
    }



    /**
     * @Description: 跨域处理
     * @param registry
     * @return void
     * @Author lhb
     * @CreateTime 2025/5/10 16:18
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
//                .allowedOrigins("*")//部署服务器再改为：http://localhost:端口号"
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }


    /**
     * @Description: 拦截器
     * @param registry
     * @return void
     * @Author lhb
     * @CreateTime 2025/5/10 16:28
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(seekerIntercept)
                .addPathPatterns("/seeker/**")
                .excludePathPatterns(
//                        "/seeker/auth/loginByPhone",
                        "/seeker/auth/login",
                        "/seeker/auth/register"
                )
                .order(0); // 优先级
        registry.addInterceptor(recruiterIntercept)
                .addPathPatterns("/recruiter/**")
                .excludePathPatterns(
//                        "/recruiter/loginByPhone",
//                        "/recruiter/register",
//                        "/recruiter/getVerifyMessage",

//                        "/recruiter/auth/loginByPhone",
                        "/recruiter/auth/login",
                        "/recruiter/auth/register",
                        "/recruiter/auth/applyRegisterCompany",
                        "/recruiter/auth/getVerifyMessage"
                )
                .order(1); // 优先级
        registry.addInterceptor(managerIntercept)
                .addPathPatterns("/manager/**")
                .excludePathPatterns(
//                        "/manager/auth/loginByPhone",
                        "/manager/auth/login"
                )
                .order(2); // 优先级
    }






}
